package com.mall.common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

/**
 * Cookie 辅助类
 * 
 */
public class CookieUtils {
	/**
	 * 每页条数cookie名称
	 */
	public static final String COOKIE_PAGE_SIZE = "_cookie_page_size";
	/**
	 * 默认每页条数
	 */
	public static final int DEFAULT_SIZE = 20;
	/**
	 * 最大每页条数
	 */
	public static final int MAX_SIZE = 200;

	/**
	 * 获得cookie
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param name
	 *            cookie name
	 * @return if exist return cookie, else return null.
	 */
	public static Cookie getCookie(HttpServletRequest request, String name) {
		Assert.notNull(request);
		Cookie[] cookies = request.getCookies();
		if (cookies != null && cookies.length > 0) {
			for (Cookie c : cookies) {
				if (c.getName().equals(name)) {
					return c;
				}
			}
		}
		return null;
	}
	
	/**
	 * 获得cookie value
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param name
	 *            cookie name
	 * @return if exist return cookie, else return null.
	 */
	public static String get(HttpServletRequest request, String name) {
		Assert.notNull(request);
		try {
			Cookie cookie = getCookie(request, name);
			if (cookie != null) {
				return URLDecoder.decode(cookie.getValue(), "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		};
		return null;
	}

	/**
	 * 根据部署路径，将cookie保存在根目录。
	 * 
	 * @param request
	 * @param response
	 * @param name
	 * @param value
	 * @param expiry
	 * @param domain
	 * @return
	 */
	public static Cookie addCookie(HttpServletRequest request,
			HttpServletResponse response, String name, String value,
			Integer expiry, String domain) {
		try {
			name = URLEncoder.encode(name, "UTF-8");
			value = URLEncoder.encode(value, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Cookie cookie = new Cookie(name, value);
		if (expiry != null) {
			cookie.setMaxAge(expiry);
		}
		if (StringUtils.isNotBlank(domain)) {
			cookie.setDomain(domain);
		}
		response.addCookie(cookie);
		return cookie;
	}
	
	public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, 
			String value, Integer maxAge, String path, String domain, Boolean secure){
		Assert.notNull(request);
		Assert.notNull(response);
		Assert.hasText(name);
		try {
			name = URLEncoder.encode(name, "UTF-8");
			value = URLEncoder.encode(value, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Cookie cookie = new Cookie(name, value);
		if (maxAge != null) {
			cookie.setMaxAge(maxAge);
		}
		if (StringUtils.isNotEmpty(path)){
			cookie.setPath(path);
		}
		if (StringUtils.isNotEmpty(domain)){
			cookie.setDomain(domain);
		}
		if (secure != null){
			cookie.setSecure(secure);
		}
		response.addCookie(cookie);
	}
	
	public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value){
		String ctx = request.getContextPath();
		addCookie(request, response, name, value, null, ctx, null, null);
	}
	
	public static void addGlobalCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, Integer maxAge){
		addCookie(request, response, name, value, maxAge, "/", null, null);
	}
	
	public static Cookie addCookie(HttpServletRequest request,
			HttpServletResponse response, Cookie cookie, Integer expiry){
		if (expiry != null) {
			cookie.setMaxAge(expiry);
		}
		response.addCookie(cookie);
		return cookie;
	}

	public static void removeCookie(HttpServletRequest request, HttpServletResponse response, 
			String name, String path, String domain){
		Assert.notNull(request);
		Assert.notNull(response);
		Assert.hasText(name);
		try {
			name = URLEncoder.encode(name, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Cookie cookie = new Cookie(name, null);
		cookie.setMaxAge(0);
		if (StringUtils.isNotEmpty(path)){
			cookie.setPath(path);
		}
		if (StringUtils.isNotEmpty(domain)){
			cookie.setDomain(domain);
		}
		response.addCookie(cookie);
		
	}
	
	/**
	 * 取消cookie
	 * 
	 * @param request
	 * @param response
	 * @param name
	 * @param domain
	 */
	public static void removeCookie(HttpServletRequest request,
			HttpServletResponse response, String name) {
		String ctx = request.getContextPath();
		removeCookie(request, response, name, ctx, null);
	}
}
